var uReadMeSaveOb = new function () {
	hst			= 'http://www.uread.me';
	bg 			= null;			// 创建的DOM对象，背景，点击可以关闭前景和背景
	fg 			= null;			// 创建的DOM对象，前景，用于显示save或者login内容
	ht_select 	= '';			// 选中的html或者全部html
	xmlhttp 	= new XMLHttpRequest();
	create_div = function () {
		bg = document.getElementById('uReadMeSaveBg');
		fg = document.getElementById('uReadMeSaveFg');
		if (bg || fg) { clos(); }

		bg = document.createElement('div');
		bg.setAttribute('id', 		'uReadMeSaveBg');
		bg.setAttribute('style',	'display:block;font:normal normal normal 12px Arial;color:#20292F;margin:0;padding:0;background:#20292F none;border:none;line-height:normal;text-align:left;text-decoration:none;white-space:normal;width:auto;height:auto;overflow:auto;cursor:pointer;list-style:none;direction:ltr;letter-spacing:normal;text-indent:0;text-transform:none;word-spacing:0;top:0;left:0;right:0;bottom:0;clear:none;clip:auto;float:none;position:fixed;vertical-align:baseline;visibility:visible;z-index:2147483647;opacity:0.7;');
		bg.onclick 					= function () { clos(); }
		document.body.appendChild(bg);
		
		fg = document.createElement('div');
		fg.setAttribute('id', 		'uReadMeSaveFg');
		fg.setAttribute('style',	'display:block;font:normal normal normal 12px Arial;color:#000;margin:0;padding:0;background:#F9F9F9 none;border-style:solid; border-width:1px; border-color:#FFF #666 #666 #FFF;line-height:normal;text-align:left;text-decoration:none;white-space:normal;width:auto;height:auto;overflow:auto;cursor:pointer;list-style:none;direction:ltr;letter-spacing:normal;text-indent:0;text-transform:none;word-spacing:0;top:50px;left:10%;right:10%;bottom:50px;clear:none;clip:auto;float:none;position:fixed;vertical-align:baseline;visibility:visible;z-index:2147483647;opacity:1;');
		document.body.appendChild(fg);
	}
	getSelectionHTML = function () {
		var s = window.getSelection();
		if (s && s.rangeCount>0 && s.toString()) {
			var clonedSelection = '';
			var div = document.createElement('div');
			for (var i=0; i<s.rangeCount; i++) {
				clonedSelection = s.getRangeAt(i).cloneRange().cloneContents();
				div.appendChild(clonedSelection);
			}
			return str_filter(div.innerHTML);
		} else return '';
	}
	getAllHTML = function () {
		if (document.readyState == 'complete') {
			return str_filter(document.body.parentNode.outerHTML);
		} else return '';
	}
	getTitle = function () {
		return str_filter(document.title);
	}
	getUrl = function () {
		return str_filter(document.URL);
	}
	str_filter = function (str) {
		str = str.replace(/<script.+?<\/script>|<style.+?<\/style>|<iframe.+?<\/iframe>|<!--.+?-->|<form.+?>|<\/form>/gim, "");
		str = str.replace(/\'/gim, "3590cb8af0bbb9e78c343b52b93773c9");
		str = str.replace(/\"/gim, "b15835f133ff2e27c7cb28117bfae8f4");
		return encodeURIComponent(str);
	}
	this.opn = function () {
		ht_select 	= getSelectionHTML();
		
		if (ht_select) {
			create_div();
			ajax_init();
		} else {
			if (document.readyState == 'complete') {
				ht_select = getAllHTML();
				create_div();
				ajax_init();
			} else {
				create_div();
				fg_waring('当前页面尚未加载完毕，无法保存整个页面！<br>您可以选定页面中的部分内容进行保存。', '#ACF');
				return false;
			}
		}
	}
	clos = function () {
		document.body.removeChild(fg);
		document.body.removeChild(bg);
	}
	this.close = function () {
		clos();
	}
	ajax_init = function (is_logout) {
		if (fg) {
			xmlhttp.onreadystatechange=function() {
				if (xmlhttp.readyState==4) {
					if (xmlhttp.status==200) {												// 初始化完成
						fg.innerHTML = xmlhttp.responseText;
						var ifr = document.getElementById('uReadMe_save_login_iframe');
						if (ifr) {			// 有iframe，是登陆页面，检查登陆是否成功
							var ifr_times = 0;
							ifr.onload = function () {
								if (ifr_times > 0) {
									ajax_is_logged_in();
								}
								ifr_times++;
							};
						} else {															// save页面
							// 绑定change folder事件、save button事件
							var sel = document.getElementById('uReadMe_saved_folder_id');
							var dd 	= document.getElementById('uReadMe_new_folder_div');
							var btn = document.getElementById('uReadMe_save_btn');
							if (sel && dd && btn) {
								sel.addEventListener("change", function() { 
									if (sel.value == '0') {
										dd.style.display = '';
									} else {
										dd.style.display = 'none';
									}
								}, false);
								btn.addEventListener("click", function() { 
									save(); 
								}, false);
							}
							// 绑定 uReadMe_circles 的click事件
							var i = 0;
							var sc = document.getElementsByName('uReadMe_circles');
							for (i=0; i<sc.length; i++) {
								sc[i].addEventListener("click", function() {
									if (this.style.backgroundColor == 'transparent') {
										this.style.backgroundColor = '#69F';
										this.style.color = "#FFF";
									} else {
										this.style.backgroundColor = 'transparent';
										this.style.color = "#069";
									}
								}, false);
							}
							var sf = document.getElementsByName('uReadMe_friends');
							for (i=0; i<sf.length; i++) {
								sf[i].addEventListener("click", function() {
									if (this.style.backgroundColor == 'transparent') {
										this.style.backgroundColor = '#69F';
										this.style.color = "#FFF";
									} else {
										this.style.backgroundColor = 'transparent';
										this.style.color = "#069";
									}
								}, false);
							}
							// 绑定logout click事件
							var lo = document.getElementById('uReadMe_exit');
							if (lo) {
								lo.addEventListener("click", function() { 
									fg_waring('退出...', '#ACF');
									ajax_init('logout');
								}, false);
							}
							// 关闭按钮
							var ic = document.getElementById('uReadMe_close');
							ic.addEventListener("click", function() { 
								clos();
							}, false);
						}
					} else fg_waring('网络连接错误！', '#FF3333');
				} else fg_waring('加载中...', '#ACF');
			}
			if (is_logout == 'logout') {
				xmlhttp.open("GET", hst+"/save.php?logout=1"+(+new Date), true);
			} else {
				xmlhttp.open("GET", hst+"/save.php?br=cr&t="+getTitle(), true);
			}
			xmlhttp.send();
		} else return false;
	}
	ajax_is_logged_in = function () {
		xmlhttp.onreadystatechange=function() {
			if (xmlhttp.readyState==4 && xmlhttp.status==200) {
				if (xmlhttp.responseText === '1') {
					ajax_init();
				}
			}
		}
		xmlhttp.open("GET", hst+"/save_is_login.php?br=cr&"+(+new Date), true);
		xmlhttp.send();
	}
	save = function () {
		if (ht_select) {
			var i	= 0;
			var ob 	= null;
			
			// -- 先获得save页面中的各个表单数据 -----------------------------------------------------------
			ob = document.getElementById('uReadMe_saved_folder_id');
			var fi = str_filter(ob.value);
			ob = document.getElementById('uReadMe_new_folder_name');
			var fn = str_filter(ob.value);
			
			ob = document.getElementsByName('uReadMe_new_folder_privacy');
			var fp = '';
			for (i=0; i<ob.length; i++) {
				if (ob[i].checked) {  fp = ob[i].value;  }
			}
			
			ob = document.getElementsByName('uReadMe_circles');
			var c = '';
			for (i=0; i<ob.length; i++) {
				if (ob[i].style.backgroundColor != 'transparent') {  c += ob[i].attributes.uReadMeCid.value + ',';  }
			}
			
			ob = document.getElementsByName('uReadMe_friends');
			var f = '';
			for (i=0; i<ob.length; i++) {
				if (ob[i].style.backgroundColor != 'transparent') {  f += ob[i].attributes.uReadMeFid.value + ',';  }
			}
			
			ob = document.getElementById('uReadMe_is_publish_to_weibo');
			var p2wb = (ob && ob.checked) ? '1' : '';
			
			// -- IFrame和Form  -----------------------------------------------------------
			ob = document.getElementById('uReadMe_save_a');
			ob.innerHTML = '<div id="uReadMeSaveWaiting" style="display:block;font:normal normal normal 24px 微软雅黑;color:#ACF;margin:0;padding:50px;background:transparent none;border:none;line-height:normal;text-align:center;text-decoration:none;white-space:normal;width:auto;height:auto;overflow:auto;cursor:auto;list-style:none;direction:ltr;letter-spacing:normal;text-indent:0;text-transform:none;word-spacing:0;top:auto;left:auto;right:auto;bottom:auto;clear:none;clip:auto;float:none;position:static;vertical-align:baseline;visibility:visible;z-index:auto;">操作中，请稍候...</div>';
			var ifr = document.createElement('iframe');
			ifr.setAttribute('style',	'display:none;font:normal normal normal 12px Arial;color:#000;margin:0;padding:0;background:transparent none;border:none;line-height:normal;text-align:left;text-decoration:none;white-space:normal;width:100%;height:'+(fg.clientHeight-100)+'px;overflow:auto;cursor:normal;list-style:none;direction:ltr;letter-spacing:normal;text-indent:0;text-transform:none;word-spacing:0;top:0;left:0;right:0;bottom:0;clear:none;clip:auto;float:none;position:static;vertical-align:baseline;visibility:visible;z-index:auto;opacity:1;');
			ifr.setAttribute('id', 			'uReadMeSaveIframe');
			ifr.setAttribute('name', 		'uReadMeSaveIframe');
			ifr.setAttribute('width', 		'100%');
			ifr.setAttribute('height', 		(fg.clientHeight - 100));
			ifr.setAttribute('frameborder', '0');
			ob.appendChild(ifr);		// 把onload放在后面，这样就是一次
			ifr.onload = function() {
				ob.removeChild(document.getElementById('uReadMeSaveWaiting'));
				ifr.style.display 				= 'block';
			}
			
			var fm = document.createElement('form');
			fm.setAttribute('id', 		'uReadMeSaveForm');
			fm.setAttribute('name', 	'uReadMeSaveForm');
			fm.setAttribute('method', 	'POST');
			fm.setAttribute('action', 	hst+'/save.php');
			fm.setAttribute('target', 	'uReadMeSaveIframe');
			
			create_hidden_input(fm, 'u', 	getUrl());
			create_hidden_input(fm, 't', 	getTitle());
			create_hidden_input(fm, 'hs', 	ht_select);
			create_hidden_input(fm, 'fi', 	fi);
			create_hidden_input(fm, 'fn', 	fn);
			create_hidden_input(fm, 'fp', 	fp);
			create_hidden_input(fm, 'f', 	f);
			create_hidden_input(fm, 'c', 	c);
			create_hidden_input(fm, 'p2wb', p2wb);
			
			ob.appendChild(fm);
			fm.submit();
		} else {
			fg_waring('无法获取页面内容，请刷新页面重新再试，谢谢！', '#F33');
		}
	}
	create_hidden_input = function (fm, id, value) {
		var input = document.createElement("input");
		input.setAttribute("type", 	"hidden");
		input.setAttribute("id", 	id);
		input.setAttribute("name", 	id);
		input.setAttribute("value", value);
		fm.appendChild(input);
	}
	fg_waring = function (msg, color) {
		if (fg) {
			fg.innerHTML = '<div style="display:block;font:normal normal normal 24px 微软雅黑;color:'+color+';margin:50px;padding:50px;background:transparent none;border:1px solid #CCC;line-height:50px;text-align:center;text-decoration:none;white-space:normal;width:auto;height:auto;overflow:auto;cursor:auto;list-style:none;direction:ltr;letter-spacing:normal;text-indent:0;text-transform:none;word-spacing:0;top:auto;left:auto;right:auto;bottom:auto;clear:none;clip:auto;float:none;position:static;vertical-align:baseline;visibility:visible;z-index:auto;">'+msg+'</div>';
		} else return false;
	}
}
uReadMeSaveOb.opn();